//
//  ZKWelcomeViewController.swift
//  Weibo
//
//  Created by 郑克哥哥 on 2017/7/26.
//  Copyright © 2017年 郑克哥哥. All rights reserved.
//

import UIKit

import SnapKit
import SDWebImage



//登录欢迎界面
class ZKWelcomeViewController: UIViewController {
    
    //MARK: -------加载控件-------
    
    fileprivate lazy var bgImgView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    fileprivate lazy var userImgView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    fileprivate lazy var label: UILabel = UILabel()
    
    //自定义视图
    override func loadView() {
        view = bgImgView
        
        
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    //视图已经显示完成时
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        springAnimation()
    }
    
    private func setupUI(){
        
        userImgView.layer.masksToBounds = true
        userImgView.layer.cornerRadius = 90 / 2
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.darkGray
        label.alpha = 0
        
        view.addSubview(userImgView)
        view.addSubview(label)
        
        if let name = ZKUserAccount.sharedViewModel.userAccount?.name {
            
            label.text = "欢迎回来,\(name)"
        }else{
            
            label.text = "欢迎回来"
        }
        if let imgUrl = ZKUserAccount.sharedViewModel.userAccount?.avatar_large {
            
            userImgView.sd_setImage(with: URL(string: imgUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        //设置约束
        userImgView.snp.makeConstraints { (make) in
            
            make.top.equalTo(view).offset(200)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        label.snp.makeConstraints { (make) in
            
            make.top.equalTo(userImgView.snp.bottom).offset(10)
            make.centerX.equalTo(userImgView)
        }
        
        
    }
    
    //图片弹簧效果
    private func springAnimation(){
        
        //更新约束要保证约束之前的存在,否则更新会崩溃
        userImgView.snp.updateConstraints { (make) in
             make.top.equalTo(view).offset(100)
        }
        
        UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: {
            self.view.layoutIfNeeded()
        }) { (_) in
            
           UIView.animate(withDuration: 1, animations: { 
            self.label.alpha = 1
           }, completion: { (_) in
            //进入首页
            
            //切换根视图控制器
            NotificationCenter.default.post(name: NSNotification.Name(SwitchRootViewController), object: nil)
           })
            
        }
        
    }

 

}
